home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / ArrayedCollection.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  4.0 KB  |  174 lines

  1. " --------------------------------------------------------------- "
  2. " Added the with: methods on 04-Apr-2002.                         "
  3. " --------------------------------------------------------------- "
  4.  
  5. Class ArrayedCollection :SequenceableCollection
  6. ! current !
  7. [
  8.     = anArray   ! i !
  9.       
  10.       amigatalk tracingOff.
  11.       
  12.       (self size ~= anArray size) 
  13.          ifTrue: [^ false]. 
  14.             
  15.       i <- 0.
  16.  
  17.       self do: [:x | (x ~= (anArray at: (i <- i + 1)))
  18.                                         ifTrue: [^ false]
  19.                ].
  20.       
  21.       amigatalk tracingOn.
  22.       
  23.       ^ true
  24. |
  25.     at: key ifAbsent: exceptionBlock
  26.  
  27.       ((key <= 0) or: [key > self size])
  28.          ifTrue: [^ exceptionBlock value].
  29.  
  30.       ^ self at: key
  31. |
  32.     coerce: aCollection      ! temp !
  33.  
  34.       temp <- self class new: aCollection size.
  35.  
  36.       temp replaceFrom: 1 to: aCollection size with: aCollection.
  37.  
  38.       ^ temp
  39. |
  40.     copyFrom: start to: stop       ! size temp !
  41.  
  42.       size <- stop - start + 1.
  43.  
  44.       temp <- self class new: size.
  45.  
  46.       temp replaceFrom: 1 to: size with: self startingAt: start.
  47.  
  48.       ^ temp
  49. |
  50.     currentKey
  51.       ^ current
  52.     deepCopy ! newobj !
  53.     
  54.       amigatalk tracingOff.
  55.       
  56.       newobj <- self class new: self size.
  57.  
  58.       (1 to: self size) do: [:i | newobj at: i put: (self at: i) copy ].
  59.  
  60.       amigatalk tracingOff.
  61.       
  62.       ^ newobj
  63. |
  64.     do: aBlock
  65.  
  66.       amigatalk tracingOff.
  67.       
  68.       (1 to: self size) 
  69.           do: [:i | current <- i. 
  70.             aBlock value: (self at: i)].
  71.  
  72.       amigatalk tracingOff.
  73. |
  74.     first
  75.  
  76.       current <- 1.
  77.  
  78.       ^ (current <= self size) 
  79.          ifTrue: [ self at: current]
  80. |
  81.     firstKey
  82.  
  83.       ^ 1
  84. |
  85.     lastKey
  86.  
  87.       ^ self size
  88. |
  89.     next
  90.  
  91.       current <- current + 1.
  92.  
  93.       ^ (current <= self size) 
  94.          ifTrue: [ self at: current]
  95. |
  96.     padTo: length
  97.  
  98.       ^ (self size < length)
  99.           ifTrue: [ self , (self class new: (length - self size) ) ]
  100.          ifFalse: [ self ]
  101. |
  102.     shallowCopy ! newobj !
  103.  
  104.       newobj <- self class new: self size.
  105.  
  106.       (1 to: self size) do: [:i | newobj at: i put: (self at: i) ].
  107.  
  108.       ^ newobj
  109. |
  110.     with: anObject ! newCollection !
  111.  
  112.       " Answer a new instance of ArrayedCollection, containing only anObject."   
  113.          
  114.       newCollection <- self new: 1.   
  115.  
  116.       newCollection at: 1 put: anObject.   
  117.  
  118.       ^ newCollection 
  119.     with: firstObject with: secondObject ! newCollection !
  120.  
  121.       " Answer a new instance of ArrayedCollection, containing firstObject
  122.       * and secondObject.
  123.       "
  124.       newCollection <- self new: 2.
  125.  
  126.       newCollection at: 1 put: firstObject.   
  127.       newCollection at: 2 put: secondObject.   
  128.    
  129.       ^ newCollection 
  130.     with: firstObject with: secondObject with: thirdObject ! newCollection !
  131.  
  132.       " Answer a new instance of ArrayedCollection, 
  133.       * containing only these three objects.
  134.       "   
  135.       newCollection <- self new: 3.   
  136.      
  137.       newCollection at: 1 put: firstObject.   
  138.       newCollection at: 2 put: secondObject.   
  139.       newCollection at: 3 put: thirdObject.   
  140.      
  141.       ^ newCollection 
  142.     with: firstObject with: secondObject with: thirdObject with: fourthObject    
  143.       ! newCollection !
  144.      
  145.       " Answer a new instance of ArrayedCollection, containing the four
  146.       * arguments as the elements.
  147.       "   
  148.       newCollection <- self new: 4.   
  149.      
  150.       newCollection at: 1 put: firstObject.   
  151.       newCollection at: 2 put: secondObject.   
  152.       newCollection at: 3 put: thirdObject.   
  153.       newCollection at: 4 put: fourthObject.   
  154.      
  155.       ^ newCollection 
  156. |     
  157.     withAll: aCollection ! newCollection index !
  158.  
  159.       " Answer a new instance of this class, whose elements are 
  160.       * the elements of aCollection. 
  161.       "   
  162.       newCollection <- self new: aCollection size.   
  163.       index         <- 0.   
  164.      
  165.       aCollection do: [:element | newCollection at: (index := index + 1)
  166.                                                put: element ].   
  167.      
  168.       ^ newCollection 
  169. ]
  170.